package com.dries.portal.module.xxshop.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.common.exception.ApiException;
import com.dries.portal.module.xxshop.entity.Actgoods;
import com.dries.portal.module.xxshop.entity.Activitytype;
import com.dries.portal.module.xxshop.entity.Goods;
import com.dries.portal.module.xxshop.entity.XXShopActivity;
import com.dries.portal.module.xxshop.mapper.XXShopActivityMapper;
import com.dries.portal.module.xxshop.service.IActgoodsService;
import com.dries.portal.module.xxshop.service.IActivitytypeService;
import com.dries.portal.module.xxshop.service.IGoodsService;
import com.dries.portal.module.xxshop.service.IXXshopActivityService;
import com.dries.portal.module.xxshop.vo.ShopActivityVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DS("xxshop")
@Service
public class XXShopActivityServiceImpl extends ServiceImpl<XXShopActivityMapper, XXShopActivity> implements IXXshopActivityService {

    @Resource
    IGoodsService goodsService;
    @Resource
    IActgoodsService actgoodsService;
    @Resource
    IActivitytypeService activitytypeService;

    @Override
    public List<ShopActivityVo> getShopActivityByShopId(Map<String, Object> params) {

        String shopId = (String) params.get("shopId");
        LambdaQueryWrapper<XXShopActivity> wrapper = new QueryWrapper<XXShopActivity>().lambda();
        if (StrUtil.isNotBlank(shopId)) {
            wrapper.eq(XXShopActivity::getDataid, shopId);
        }

        List<XXShopActivity> xxShopActivities = list(wrapper);

        if (CollUtil.isNotEmpty(xxShopActivities)) {
            return xxShopActivities.stream().map(activity -> {
                ShopActivityVo shopActivityVo = new ShopActivityVo();
                BeanUtils.copyProperties(activity, shopActivityVo);

                // 关联活动Type
                Activitytype activitytype = activitytypeService.getById(activity.getActtypeid());
                if (activitytype != null) {
                    shopActivityVo.setActivityType(activitytype);
                }

                // 关联活动商品
                List<Actgoods> actgoods = actgoodsService.list(new QueryWrapper<Actgoods>().lambda().eq(Actgoods::getActid, activity.getActid()));
                if (CollUtil.isNotEmpty(actgoods)) {
                    shopActivityVo.setActgoods(actgoods);
                    List<Long> collect = actgoods.stream().map(Actgoods::getGoodsid).collect(Collectors.toList());
                    List<Goods> goods = goodsService.listByIds(collect);
                    if (CollUtil.isNotEmpty(goods)) {
                        shopActivityVo.setGoods(goods);
                    }
                }
                return shopActivityVo;
            }).collect(Collectors.toList());
        }

        return null;
    }

    @Override
    public ShopActivityVo getShopActivityInfoById(Long activityId) {
        ShopActivityVo shopActivityVo = new ShopActivityVo();
        XXShopActivity shopActivity = getById(activityId);
        if (shopActivity != null) {
            BeanUtils.copyProperties(shopActivity, shopActivityVo);
        } else {
            throw new ApiException("店铺活动不存在");
        }

        // 关联活动Type
        Activitytype activitytype = activitytypeService.getById(shopActivity.getActtypeid());
        if (activitytype != null) {
            shopActivityVo.setActivityType(activitytype);
        }

        // 关联活动商品
        List<Actgoods> actgoods = actgoodsService.list(new QueryWrapper<Actgoods>().lambda().eq(Actgoods::getActid, shopActivity.getActid()));
        if (CollUtil.isNotEmpty(actgoods)) {
            shopActivityVo.setActgoods(actgoods);
            List<Long> collect = actgoods.stream().map(Actgoods::getGoodsid).collect(Collectors.toList());
            List<Goods> goods = goodsService.listByIds(collect);
            if (CollUtil.isNotEmpty(goods)) {
                shopActivityVo.setGoods(goods);
            }
        }

        return shopActivityVo;
    }

}